﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.CustomControls
{
	using System;
	using System.Drawing;
	using System.Windows.Forms;

	public partial class ColorSelector : UserControl
	{
		#region Constructors

		public ColorSelector()
		{
			InitializeComponent();
			FillComboBoxList();
			comboBox1.SelectedIndex = 0;
		}

		#endregion Constructors

		#region Properties

		public System.Drawing.Color SelectedColor
		{
			get{
				if (comboBox1.SelectedIndex > 0) {
					string SelectedColorStr = comboBox1.SelectedValue.ToString();
					System.Drawing.ColorConverter converter = new System.Drawing.ColorConverter();
					System.Drawing.Color objColor =
					        (System.Drawing.Color)converter.ConvertFromString(SelectedColorStr);
					return objColor;
				} else {
					return new System.Drawing.Color();
				}
			}
			set{
				string Color = value.Name;
				ListViewItem li = FindByValue(Color); //comboBox1.Items.FindByValue(Color);
				if (li != null) {
					//comboBox1.ClearSelection();
					li.Selected = true;
				}
			}
		}

		#endregion Properties

		#region Methods

		private void FillComboBoxList()
		{
			KnownColor[] colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
			foreach (KnownColor c in colors) {
				ListViewItem item = new ListViewItem();
				item.Text = c.ToString();
				comboBox1.Items.Add(item.Text);
			}
		}

		private ListViewItem FindByValue(string color)
		{
			for (int i = 0; i < comboBox1.Items.Count; i++) {
				if (comboBox1.Items[i].Equals(color))
					return (ListViewItem)comboBox1.Items[i];
			}
			return null;
		}

		private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
		{
			this.panel1.BackColor = Color.FromName(comboBox1.SelectedItem.ToString());
		}

		#endregion Methods
	}
}